package com.papper.common.service.base;

import au.com.bytecode.opencsv.CSVWriter;
import com.google.common.collect.Lists;
import com.papper.common.bean.base.BaseDatabaseConfig;
import com.papper.common.bean.base.BaseDatabaseTransferTask;
import com.papper.common.cache.local.PapperLRUCache;
import com.papper.common.config.PapperStorage;
import com.papper.common.mapper.base.BaseDatabaseSnapshotMapper;
import com.papper.common.mapper.base.BaseDatabaseTransferTaskMapper;
import com.papper.common.service.ServiceImpl;
import com.papper.common.utils.tool.DateTool;
import com.papper.common.utils.tool.StringTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @Author:LaJiao
 * @Date:2022-03-29 10:06:49
 * @Description:系统数据迁移任务
 */
@Service("baseDatabaseTransferTaskService")
public class BaseDatabaseTransferTaskService extends ServiceImpl<BaseDatabaseTransferTaskMapper, BaseDatabaseTransferTask> {
    private final static PapperLRUCache<String, String> tableCache = new PapperLRUCache<String, String>(1000);
    private final static PapperLRUCache<String, Object> localCache = new PapperLRUCache<String, Object>(1000);
    private static final int                            STEP_COUNT = 1000;

    @Autowired
    BaseDatabaseTransferTaskMapper databaseTransferTaskMapper;
    @Autowired
    BaseDatabaseSnapshotMapper     databaseSnapshotMapper;

    public void createTransferTask(BaseDatabaseConfig config) {
        //TODO
    }

    public String existsTable(String dbspace, String tableName) {
        String key         = dbspace + "." + tableName;
        String dbTableName = tableCache.get(key);
        if (dbTableName != null) {
            return dbTableName;
        }
        dbTableName = existsTable(dbspace, tableName);
        if (dbTableName != null) {
            tableCache.put(key, dbTableName);
        }
        return dbTableName;
    }

    public int createTransferTable(String dbspace, String sourceTable, String targetTable) {
        //TODO
        return 0;
    }

    public void transfer(BaseDatabaseTransferTask task) {
        //TODO
    }

    public void clean(BaseDatabaseTransferTask task) {
        //TODO
    }

    public void fileSave(BaseDatabaseTransferTask task) {
        //TODO
    }

    private void csvTitle(CSVWriter csvWriter) {
        List<String> titles = Lists.newArrayList();
        titles.add("数据信息");
        csvWriter.writeNext(titles.toArray(new String[titles.size()]));
    }

    private void csvRecord(CSVWriter csvWriter, String dataJson) {
        List<String> rowData = Lists.newArrayList();
        rowData.add(dataJson);
        csvWriter.writeNext(rowData.toArray(new String[rowData.size()]));
    }

    /**
     * 生成文件
     */
    public static File mkdirs(String filePath, String realpath) {
        // 生成目录
        File myFilePath = new File(filePath);
        if (!myFilePath.exists()) {
            myFilePath.mkdirs();
        }
        File file = new File(realpath);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                return null;
            }
        }
        return file;
    }

    public static String dumpExcelPath(String fileName) {
        String        realpath = "";
        StringBuilder filePath = new StringBuilder();
        filePath.append(StringTool.nullToEmpty(PapperStorage.SystemConfig("systemFileExcelPath"))).append("/").append("/transfer/");
        filePath.append(DateTool.formatDate(new Date(), "YYYY")).append("/");
        filePath.append(DateTool.formatDate(new Date(), "MM"));
        if (filePath.toString().endsWith("/")) {
            realpath = filePath.toString() + fileName;
        } else {
            realpath = filePath.toString() + "/" + fileName;
        }
        mkdirs(filePath.toString(), realpath);
        return realpath;
    }

}
